package com.zhimeng.personal.activities;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.zhimeng.personal.R;
import com.zhimeng.personal.adapters.WorkinfoAdapter;
import com.zhimeng.personal.models.Workinfo;
import com.zhimeng.personal.utils.ApiUtils;
import com.zhimeng.personal.utils.ViewUtils;
import com.zhimeng.zmasynchttp.HTTPMethod;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class WorkinfosActivity extends BaseActivity {

    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;
    private List<Workinfo> workinfos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_workinfos);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_workinfos, menu);
        return true;
    }

    private String workType = "worked_companies";

    @Override
    public void initData() {
        super.initData();
        backgroundHttpUtils.startSendHttp(this, ApiUtils.WORK_INFOS + "?type=" + workType);
    }

    public void handleDataFromJson(String urlId, boolean success, JSONObject data) {
        if (urlId.startsWith(HTTPMethod.GET + ApiUtils.WORK_INFOS)) {
            workinfos = Workinfo.parseWorkinfosFromData(data);

            mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
            mViewPager = (ViewPager) findViewById(R.id.container);
            mViewPager.setAdapter(mSectionsPagerAdapter);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.interviewed_work) {
            setInterviewedWork();
        } else if (id == R.id.worked_work) {
            setWorkedWorkinfos();
        } else if (id == android.R.id.home) {
            myFinish();
        }

        return super.onOptionsItemSelected(item);
    }

    public void setWorkedWorkinfos() {

    }

    public void setInterviewedWork() {

    }


    public static class PlaceholderFragment extends Fragment {
        private static final String ARG_SECTION_NUMBER = "section_number";

        private RecyclerView workinfoRecyclerView;
        private RecyclerView.Adapter workinfosAdapter;
        private List<Workinfo> workinfosInFragment;

        public PlaceholderFragment() {
        }

        public static PlaceholderFragment newInstance(List<Workinfo> ws) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            fragment.workinfosInFragment = ws;
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_workinfos, container, false);
            workinfoRecyclerView = (RecyclerView) rootView.findViewById(R.id.workinfo_recycler_view);
            ViewUtils.setRecycleviewVerticle(getContext(), workinfoRecyclerView);

            workinfosAdapter = new WorkinfoAdapter(workinfosInFragment);
            workinfoRecyclerView.setAdapter(workinfosAdapter);

            return rootView;
        }
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            List<Workinfo> loveWorks = new ArrayList<>();
            List<Workinfo> myWorks = new ArrayList<>();

            for (Workinfo workinfo : workinfos) {
                if (workinfo.isLaopo()) {
                    loveWorks.add(workinfo);
                } else {
                    myWorks.add(workinfo);
                }
            }
            return PlaceholderFragment.newInstance(position == 0 ? myWorks : loveWorks);
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "SECTION 1";
                case 1:
                    return "SECTION 2";
            }
            return null;
        }
    }
}
